import indexEdit from '@/views/system/dict/page/indexEdit.vue'
export default {
  components: {
    indexEdit,
  },
  data() {
    return {
      params: {
        page: 1,
        limit: this.$store.state.PAGE_SIZE,
        dictKey: null,
      },
      //选择的数据
      multipleId: [],
      //表格数据
      list: [],
      //头部按钮权限
      headBtn: [],
      //表头展示列
      columns: [{
          prop: 'dictKey',
          label: '字典名称',
          formatter: (row, column, cellValue) => {
            return this.common.isNull(cellValue)
          }
        },
        {
          prop: 'type',
          label: '字典类型',
          formatter: (row, column, cellValue) => {
            return this.common.transformDict(cellValue, JSON.parse(this.$store.state.ps.DICT_LIST.dictType))
          }
        },
        {
          prop: 'des',
          label: '配置描述',
          formatter: (row, column, cellValue) => {
            return this.common.isLongToShort(cellValue)
          }
        },
      ],
      // 按钮展示列
      operates: {
        width: 150,
        fixed: 'right',
        list: []
      },
      //数据总数量
      total: 0,
      //设置分页参数
      pagination: {
        pageIndex: 1,
        pageSize: 10
      },
      //表格基本参数
      options: {
        stripe: false, // 是否为斑马纹 table
        loading: true, // 是否添加表格loading加载动画
        highlightCurrentRow: true, // 是否支持当前行高亮显示
        mutiSelect: true, // 是否支持列表项选中功能
        isFixed: true, //是否固定表格
        tableHeight: 330, //固定高度的差值
      } // table 的参数
    }
  },
  methods: {
    //初始化
    init() {
      let that = this;
      that.options.loading = true;
      that.$api.systemRequest.dictPage(that.params)
        .then(res => {
          that.options.loading = false;
          res = res.data
          if (res.code == 0) {
            that.list = res.data.pageList;
            that.total = Number(res.data.total)
          } else {
            this.$notify.error({
              title: '错误提示',
              message: res.msg
            });
          }
        })
    },
    //查询
    search() {
      let that = this;
      that.params.page = 1;
      that.pagination.pageIndex = 1;
      that.init();
    },
    //重置查询
    reload() {
      let that = this;
      that.params = {
        page: 1,
        limit: this.$store.state.PAGE_SIZE,
      }
      that.pagination.pageIndex = 1;
      that.init();
    },
    //新增
    handleAdd(path) {
      this.$refs.indexEdit.init();
    },
    // 详情
    handleDetail(index, row, path) {
      this.$refs.indexDetail.init(row.dictId);
    },
    // 编辑
    handleEdit(index, row, path) {
      this.$refs.indexEdit.init(row.dictId);
    },
    //批量删除
    handleDels() {
      let that = this;
      that.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: 'true'
      }).then(() => {
        if (that.multipleId.length > 0) {
          that.options.loading = true;
          that.$api.systemRequest.dictDeletes({
              ids: that.multipleId
            })
            .then(res => {
              res = res.data
              that.options.loading = false;
              if (res.code == 0) {
                that.search();
                this.$notify.success({
                  title: '成功提示',
                  message: res.msg
                });
              } else {
                this.$notify.error({
                  title: '错误提示',
                  message: res.msg
                });
              }
            })
        } else {
          that.$message.warning("请选择要删除的数据");
        }
      }).catch(() => {
        that.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 删除
    handleDel(index, row) {
      let that = this;
      that.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: 'true'
      }).then(() => {
        that.options.loading = true;
        that.$api.systemRequest.dictDelete({
            id: row.dictId
          })
          .then(res => {
            res = res.data
            that.options.loading = false;
            if (res.code == 0) {
              that.search();
              this.$notify.success({
                title: '成功提示',
                message: res.msg
              });
            } else {
              this.$notify.error({
                title: '错误提示',
                message: res.msg
              });
            }
          })
      }).catch(() => {
        that.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 切换每页显示的数量
    handleSizeChange(pagination) {
      this.pagination = pagination
      this.params.limit = this.pagination.pageSize
      this.init();
    },
    // 切换页码
    handleIndexChange(pagination) {
      this.pagination = pagination
      this.params.page = this.pagination.pageIndex
      this.init();
    },
    // 选中行
    handleSelectionChange(val) {
      let that = this;
      let str = "";
      let strname = "";
      val.forEach(function(item) {
        str += item.dictId + ",";
      })
      if (str.length > 0) {
        that.multipleId = str.substr(0, str.length - 1);
      } else {
        that.multipleId = []
      }
    },
  },
  mounted() {
    let that = this
    that.init();
    var oper = this.powerCommon.powerSet(that.$store.state.ps.PERM_BTN, that);
    that.operates.list = oper.tableOper
    that.headBtn = oper.headerOper
  }
}
